package com.quasar.hpatient.view.adapter;

import android.text.style.ForegroundColorSpan;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.model.medicalmodel.SelectFriendsData;
import com.quasar.hpatient.network.RetrofitConnection;
import com.quasar.hpatient.utils.Spanny;
import com.quasar.hpatient.utils.TimeZoneUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * author : 许建宏
 * Date: 2017/10/13 0013
 * View:查找好友
 */

public class AddFriendsAdapter extends BaseQuickAdapter<SelectFriendsData, BaseViewHolder> {

    public AddFriendsAdapter() {
        super(R.layout.friend_item_follow);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final SelectFriendsData item) {
        if (item.getList() != null) {
            //姓名
            if (item.getList().getRealName() != null) {
                helper.setText(R.id.name, item.getList().getRealName());
            }

            if(item.getList().getCover()!=null && item.getList().getCover().length()>0){
                Glide.with(mContext).load(RetrofitConnection.PHOTOURL+item.getList().getCover()).into((ImageView) helper.getView(R.id.headimg));
            }

            String Sex;
            //性别
            if (item.getList().getSex() == 0) {
                Sex = "女";
            } else {
                Sex = "男";
            }
            Spanny spanny = new Spanny();
            spanny.append("性别：", new ForegroundColorSpan(color(R.color.btcolor)));
            spanny.append("" + Sex, new ForegroundColorSpan(color(R.color.textcolor)));
            helper.setText(R.id.tv_job_title, item.getList().getRealName());

            //出生日期
            String dayTime;

            if(item.getList().getBirthday()!=null && item.getList().getBirthday().length()>0){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = null;
                try {
                    date = sdf.parse(item.getList().getBirthday());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                int age = TimeZoneUtil.getAge(date);
                dayTime=age+"";
            }else{
                dayTime="未知";
            }

            Spanny spanny1 = new Spanny();
            spanny1.append("年龄：", new ForegroundColorSpan(color(R.color.btcolor)));
            spanny1.append("" + dayTime, new ForegroundColorSpan(color(R.color.textcolor)));
            helper.setText(R.id.tv_hospital, spanny1);
            if (item.getData2() != null) {
                if (item.getData2().getStatus() == 0 || item.getData2().getStatus() == 2) {
                    helper.setVisible(R.id.ll_layout, false);
                }
            }else{
                helper.setVisible(R.id.ll_layout, true);
            }
            //判断如果getid等于当前用户的id则隐藏添加按钮
            if (item.getList().getId() == MainAppliaction.getInstance().getPatient().getId()) {
                helper.setVisible(R.id.ll_layout, false);
            }

            //添加好友
            helper.addOnClickListener(R.id.tv_addto);
        }
    }

    private int color(int value) {
        return MainAppliaction.getInstance().getResources().getColor(value);
    }
}
